import { request } from '@/utils/request'
import type { LoginRequest, RegisterRequest, ChangePasswordRequest, UpdateProfileRequest } from '@/types/common'

export interface LoginResponse {
  token: string
  userInfo: {
    id: number
    openId?: string
    nickname: string
    avatarUrl?: string
    gender: number
    genderText?: string
    country?: string
    province?: string
    city?: string
    language?: string
    phone?: string
    email?: string
    status: number
    statusText?: string
    userType: number
    userTypeText?: string
    points: number
    studyProgress: number
    lastLoginTime?: string
    lastLoginIp?: string
    remark?: string
    createTime: string
    updateTime: string
  }
  tokenType: string
  expiresIn: number
}

export const authApi = {
  // 网页端登录
  login(data: LoginRequest) {
    return request.post<LoginResponse>('/auth/web-login', data)
  },

  // 注册
  register(data: RegisterRequest) {
    return request.post<LoginResponse>('/auth/register', data)
  },

  // 获取用户信息
  getUserInfo() {
    return request.get('/auth/userinfo')
  },

  // 更新用户信息
  updateUserInfo(data: UpdateProfileRequest) {
    return request.put('/auth/userinfo', data)
  },

  // 修改密码
  changePassword(data: ChangePasswordRequest) {
    return request.put('/auth/password', data)
  },

  // 刷新token
  refreshToken() {
    return request.post<{ token: string; expiresIn: number }>('/auth/refresh-token')
  },

  // 登出
  logout() {
    return request.post('/auth/logout')
  }
}
